package com.hdaccp.gymxmjpa.Service;


import com.hdaccp.gymxmjpa.Dao.StaffDao;
import com.hdaccp.gymxmjpa.entity.Equipment;
import com.hdaccp.gymxmjpa.entity.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.Map;

@Service
public class StaffDaoImpl {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private StaffDao staffDao;


    public Map<String,Object> query(Map<String,Object> map1){
        //分页
        String jpal="from Staff where 1=1";
        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpal=jpal+" and staffname like '%"+map1.get("hyname")+"%'";
        }

        Query qu=entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((int)map1.get("qi"));
        //结束页数
        qu.setMaxResults((int)map1.get("shi"));

        //查询多少条数据
        String jpa="select count(m) from Staff m where 1=1";

        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpa=jpa+" and staffname like '%"+map1.get("hyname")+"%'";
        }

        Long count=(Long) entityManager.createQuery(jpa).getSingleResult();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",count);
        map.put("rows",qu.getResultList());
        return map;
    }

    //删除
    public int del(int staffId){
        Long memid=new Long(staffId);
        staffDao.deleteById(memid);
        return 1;
    }

    //增加
    public int insert(Staff staff){
        staffDao.save(staff);
        return  1;
    }

    //修改
    public int upd(Staff staff){
        staffDao.save(staff);
        return 1;
    }
    public int update(Staff staff){
        Staff staff1 =staffDao.findById(staff.getStaffId()).get();

        staffDao.save(staff1);
        return 1;
    }
}
